

#pragma once

#include "stdint.h"

/////////////////SYSTEM/////////////////
#define SCREEN 0
#define NFC 1

typedef enum ble_state_e
{
    DISCONNECT_BLE,
    INITING_BLE,
    CONNECTED_BLE,
} ble_state_t;

// 事件
typedef enum rcs_event_class_e
{
    NO_EVENT,
    CONNECT_SUCCESS,
    REMOTE_COMM_EVENT,
    BE_HITTED,
    HIT_SUCCESS,
} rcs_event_class_t;

typedef struct rcs_event_s
{
    rcs_event_class_t class;
    uint8_t count;
} rcs_event_t;

#define BLUETOOTH_DEVICE_ADDR_LEN 6

typedef uint8_t bd_addr_t[BLUETOOTH_DEVICE_ADDR_LEN];

typedef struct device_s
{
    // dev_state_t state;
    bd_addr_t cli_bda;
    uint8_t battery;
    bool low_batt;
    ble_state_t is_connect_master;
    bool wait_new_master;
    bool is_hold_scr;
    bool is_hold_nfc;
} device_t;
#define LOW_BATTERY_THRESHOLD 30

typedef uint32_t player_id_t;
typedef int16_t bullet_num_t;
typedef int16_t player_hp_t;
typedef int16_t player_damage_t;

typedef struct player_s
{
    uint8_t team;           // 9
    uint8_t room;           // 9
    player_damage_t damage; // 9999
    player_hp_t hp;         // 9999
    player_id_t id;         // 目前用设备的id等于用户的id，也等于玩家的id
    uint8_t kill;
    uint8_t death;
    int16_t team_score;
    bullet_num_t bullet_nums; // 9999
    bool living;              // 游戏中状态
} player_t;

typedef struct game_s
{
    bool start;
    // uint32_t record_id;   //游戏记录id,每一局游戏这个id不一样,slave不需要
    player_t player;
} game_t;

typedef struct rcs_slave_system_s
{
    device_t device;
    game_t game;
    rcs_event_t event;
} rcs_slave_system_t;

void take_mutex(void);
void give_mutex(void);

void game_event_init(void);
void set_game_event(rcs_event_class_t _class, uint8_t _count);
void update_game_event(void);
rcs_event_t *get_game_event_state(void);
/////////////////GAME/////////////////
uint8_t *make_verify_package(uint8_t *pack_len);
uint8_t *make_disconnect_package(uint8_t *pack_len);
uint8_t *make_command_package(uint8_t command, uint8_t *pack_len);
uint8_t *make_be_hitted_package(player_t *enemy, uint8_t batt);

void analyse_master_package(uint8_t *buf, uint8_t buf_len);

//////////////////shock//////////////////

#define SHOCK_OUTPUT_PIN 5

void shock_init(void);
void set_shock(bool state);

//////////////////////////POWER//////////////////////////
#define POWER_PIN 10
void power_init(void);
void power_off(void);

//////////////////////////BATTERY//////////////////////////
#define CALI 1
void rcs_adc_battery_init(void);
uint8_t rcs_adc_battery_read(void);
